{include file="public/layout" /}
<style>
    .ncap-form-default dd.opt {
        width: 200px;
    }
    .ncap-form-default dd.opt {
        width: 200px;
    }
    #SpecTempLateDiv table b {
        display: inline-block;
        width: 130px;
    }
    div.bot3 {
        left: 0;
        right: 0;
    }
</style>
<body style="background-color: rgb(255, 255, 255); overflow: auto; cursor: default; -moz-user-select: inherit;min-width:400px;display: inline-table">
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page pt0" style="box-shadow: none; min-width: 400px;">
    <div class="flexigrid" style="display: inline-block">
        <form class="form-horizontal" id="goodsPost">
            <input type="hidden" name="aid" value="{$goods.aid}">
            <input type="hidden" name="openSpec" value="{$openSpec}">
            <div class="ncap-form-default" style="display: inline-block">
                <!-- 未开启规格 -->
                <div {notempty name="$htmlTable"} style="display: none;" {/notempty}>
                    <dl class="row">
                        <dt class="tit"> <label for="users_price">{if condition="1 == $shopConfig['shop_open']"}<em>*</em>{/if}价格</label> </dt>
                        <dd class="opt">
                            <input type="text" name="users_price" id="users_price" maxlength="100" placeholder="原价" style="width: 180px;" value="{$goods.users_price}" onkeyup="this.value=this.value.replace(/[^\.\d]/g,'');" onpaste="this.value=this.value.replace(/[^\.\d]/g,'');">
                        </dd>
                    </dl>
                    <dl class="row">
                        <dt class="tit"> <label for="stock_count">库存</label> </dt>
                        <dd class="opt">
                            <input type="text" name="stock_count" id="stock_count" style="width: 180px;" value="{$goods.stock_count}" onkeyup="this.value=this.value.replace(/[^\d]/g,'');" onpaste="this.value=this.value.replace(/[^\d]/g,'');">
                            <!-- <label><input type="checkbox" name="stock_show" value='1' {eq name="$goods.stock_show" value="1"} checked {/eq}>页面显示商品库存</label> -->
                        </dd>
                    </dl>
                    <dl class="row">
                        <dt class="tit"> <label for="virtual_sales">虚拟销量</label> </dt>
                        <dd class="opt">
                            <input type="text" name="virtual_sales" id="virtual_sales" style="width: 180px;" value="{$goods.virtual_sales}" onkeyup="this.value=this.value.replace(/[^\d]/g,'');" onpaste="this.value=this.value.replace(/[^\d]/g,'');">
                        </dd>
                    </dl>
                </div>
                <!-- 已开启规格 -->
                <div {empty name="$htmlTable"} style="display: none;" {/empty}>
                    <div id="SpecTempLateDiv">{$htmlTable}</div>
                    <script type="text/javascript">
                        $(function () {
                            var stock_count = 0;
                            $('.stock_count').each(function() {
                                stock_count +=+ $(this).val();
                            })
                            if (0 === parseInt(stock_count)) stock_count = '{$goods.stock_count}';
                            $('#stock_count').val(stock_count);
                            // 取最小值价格
                            UpPrice();
                            // 合并单元格
                            mergeCells();
                        });

                        // 更新价格，取出最小的值赋值到价格中
                        function UpPrice(obj) {
                            var usersPriceArray = new Array();
                            $('.users_price').each(function(){
                                usersPriceArray.push($(this).val());
                            });
                            var minValue = '{$goods.users_price}';
                            if (usersPriceArray.length != 0) {
                                // 取出最小值
                                minValue = Math.min.apply(Math, usersPriceArray);
                                // 禁用库存框
                                $('#users_price').attr('readonly', 'readonly');
                            }
                            $('#users_price').val(minValue);
                        }

                        // 批量设置价格
                        function BulkSetPrice(obj) {
                            layer.prompt({
                                formType: 3,
                                shade: layer_shade,
                                id: 'BulkSetPrice',
                                title: '批量设置价格',
                                btn: ['确定', '关闭'],
                                btnAlign:'r',
                                closeBtn: 3,
                                success: function(layero, index){
                                    $("#BulkSetPrice").find('input').attr('placeholder', '批量设置价格');
                                    $("#BulkSetPrice").find('input').attr('onkeyup', "this.value=this.value.replace(/[^\\d.]/g,'')");
                                    $("#BulkSetPrice").find('input').attr('onpaste', "this.value=this.value.replace(/[^\\d.]/g,'')");
                                }
                            }, function(price, index) {
                                layer.close(index);
                                // 规格中的价格
                                $('.users_price').val(price);
                                // 产品价格
                                $('#users_price').val(price);
                            });
                        }

                        // 批量设置划线价
                        function BulkSetCrossedPrice() {
                            layer.prompt({
                                title: '批量设置划线价', 
                                shade: layer_shade,
                                formType: 3,
                                id: 'BulkSetCrossedPrice',
                                btn: ['确定', '关闭'],
                                closeBtn: 0,
                                success: function(layero, index){
                                    $("#BulkSetCrossedPrice").find('input').attr('placeholder', '批量设置划线价');
                                    $("#BulkSetCrossedPrice").find('input').attr('onkeyup', "this.value=this.value.replace(/[^\\d.]/g,'')");
                                    $("#BulkSetCrossedPrice").find('input').attr('onpaste', "this.value=this.value.replace(/[^\\d.]/g,'')");
                                }
                            }, function(price, index){
                                layer.close(index);
                                // 规格中的划线价
                                $('.crossed_price').val(price);
                                // 商品划线价
                                $('#crossed_price').val(price);
                            });
                        }

                        // 批量设置库存
                        function BulkSetStock(obj) {
                            layer.prompt({
                                formType: 3,
                                shade: layer_shade,
                                id: 'BulkSetStock',
                                title: '批量设置库存',
                                btn: ['确定', '关闭'],
                                btnAlign:'r',
                                closeBtn: 3,
                                success: function(layero, index){
                                    $("#BulkSetStock").find('input').attr('placeholder', '批量设置库存');
                                    $("#BulkSetStock").find('input').attr('onkeyup', "this.value=this.value.replace(/[^\\d.]/g,'')");
                                    $("#BulkSetStock").find('input').attr('onpaste', "this.value=this.value.replace(/[^\\d.]/g,'')");
                                }
                            }, function(stock, index) {
                                layer.close(index);
                                // 单个库存
                                $('.stock_count').val(stock);
                                $('.stock_count').attr('data-old_stock',stock);
                                stock_count = Number($('.stock_count').length) * Number(stock);
                                $('#stock_count').val(stock_count);
                                // 禁用库存框
                                $('#stock_count').attr('readonly', 'readonly');
                            });
                        }

                        // 保留操作方法，仅防止JS报错
                        function setProductSpecPrice() {
                            // .......
                        }

                        // 合并单元格
                        function mergeCells() {
                            var tab = document.getElementById("spec_input_tab");
                            var maxCol = 2, val, count, start; 
                            if (tab != null) {
                                for (var col = maxCol - 1; col >= 0; col--) {
                                    count = 1;
                                    val = "";
                                    for (var i = 0; i < tab.rows.length; i++) {
                                        if (val == tab.rows[i].cells[col].innerHTML) {
                                            count++;
                                        } else {
                                            if (count > 1) { //合并
                                                start = i - count;
                                                tab.rows[start].cells[col].rowSpan = count;
                                                for (var j = start + 1; j < i; j++) {
                                                    tab.rows[j].cells[col].style.display = "none";
                                                }
                                                count = 1;
                                            }
                                            val = tab.rows[i].cells[col].innerHTML;
                                        }
                                    }
                                    if (count > 1) { //合并，最后几行相同的情况下
                                        start = i - count;
                                        tab.rows[start].cells[col].rowSpan = count;
                                        for (var j = start + 1; j < i; j++) {
                                            tab.rows[j].cells[col].style.display = "none";
                                        }
                                    }
                                }
                            }
                        }
                    </script>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="bot3 wm100">
    <a href="javascript:void(0);" onclick="saveGoods();" class="ncap-btn-big ncap-btn-green"><span>确认</span></a>
    <a class="ncap-btn-big ncap-btn-no" href="javascript:void(0);" onclick="window.parent.layer.closeAll();" title="关闭">关闭</a>
</div>

<script type="text/javascript">
    function saveGoods() {
        layer_loading('正在处理');
        $.ajax({
            type: "POST",
            url : "{:url('ShopProduct/goods_quick_edit', ['_ajax'=>1])}",
            data: $('#goodsPost').serialize(),
            dataType: 'json',
            success: function (res) {
                layer.closeAll();
                if (1 === parseInt(res.code)) {
                    showSuccessMsg(res.msg, 1500, function() {
                        parent.window.location.reload();
                    });
                } else {
                    showErrorAlert(res.msg);
                }
            },
            error: function(e) {
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    }
    // var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    // parent.layer.iframeAuto(index);
</script>